#region File Description
//  DomoBCI 
//  License: GNU/GPL
//  Developer: Federico Leonardo Motta
//  File description: This component handles the Stimulation.
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SharedContent;
using DomoBCI.Core.GameManager;
using DomoBCI.Core.Stimulation.Tipi;
using DomoBCI.Core.Stimulation.External;
using DomoBCI.Core.Stimulation.Internal;
#endregion


namespace DomoBCI.Core.GameManager
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class StimulationManager : Microsoft.Xna.Framework.GameComponent
    {
        LEDDriver led;
        Square square;
        Scacchiera chess;
        StorageDevice device;
        Boolean isExternal;

        public StimulationManager(Game game)
            : base(game)
        {
            FileManager fm = new FileManager();
            SharedOptions opt = new SharedOptions();
            try
            {
                opt = fm.GetOptions(device);
                isExternal = opt.Stimolation;
            }
            catch (System.IO.FileNotFoundException)
            {
            }
            //TODO:Aggiungere nelle opzioni la voce Square/Chess e una condizione qua sotto
            led = new LEDDriver();
            square = new Square(Game);
        }


        public override void Initialize()
        {


            base.Initialize();
        }


        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public void Init() {
            if(isExternal){
                if (led.init("COM1") == 0 && led.isOpen)
                {
                    led.start();
                }
            }else{
                Game.Components.Add(square);
            }
        }
        public void Start() {
            if (isExternal)
            {
                if (led.isOpen)
                {
                    led.start();
                }
            }
            else
            {
                square.Start();
            }
        }

        public void Stop() {
            if(isExternal){
                if (led.IsOn)
                {
                    led.stop();
                }
            }else{
                square.Pause();
                //Game.ComponentDispose(square);
            }
        }

        public void SetFrequencies(int l, int r, int u, int d)
        {
            if (isExternal)
            {
                led.SetFrequencies(l, r, u, d);
            }
            else {
                square.SetFrequencies(l, r, u, d);
                Console.WriteLine("left: "+ l + "\t" + "right: " + r + "\nup: " + u +"\t"+ "down: "+d);
            }
        }

        public Boolean IsExternal {
            private set { isExternal = value; }
            get { return isExternal; }
        }
    }
}
